<template>
  <div class="main-container">
    <div
      :infinite-scroll-disabled="scroll.disabled"
      :infinite-scroll-distance="50"
      :infinite-scroll-delay="300"
      immediate-check="false"
      class="msg-window"
      id="msg-window"
      @scroll="onListScroll"
      @click="onListClick"
      style=""
    >
      <div id="menu" class="menu" v-show="touchMenuVisible">
        <div class="menu-item" @click="deleteMsg">删除</div>
      </div>

      <div
        class="load-msg-btn"
        @click="loadMsgBtnClick"
        v-show="!scroll.noMore"
        style="
          width: 2.5rem;
          font-size: 0.3rem;
          text-align: center;
          border-radius: 0.5rem;
          margin: auto;
          line-height: 0.75rem;
          background: white;
          margin-bottom: 0.3rem;
        "
      >
        加载更多
      </div>

      <div
        v-show="scroll.noMore"
        style="font-size: 0.3rem; color: #333; width: 100%; text-align: center;padding:0.35rem;"
      >
        没有更多消息了~
      </div>

      <!-- <div class="msg-list"> -->
         
      <div
        class="msg-item"
        v-for="item in dataList"
        :id="'msg-item-' + item.id"
        @touchstart="touchstart($event, item)"  
        @touchend="touchend"
      
      >
        <div class="msg-item-top">
          <p class="date">{{ dateFormat(item.date) }}</p>
        </div>
        <!-- msg-item-top end -->
        <div class="body" :class="{ 'row-reverse': item.isSelf == 1 }">
          <div class="left">
            <img :src="item.avatar" class="avatar" />
          </div>
          <!-- <img class="avatar" /> -->
          <div class="right">
            <p class="name">{{ item.name }}</p>
            <div class="msg-info">
              <template v-if="item.isPicture">
                <img class="msg-img" :src="item.picture" />
              </template>
              <template v-else>
                {{ item.content }}
              </template>
            </div>
          </div>
        </div>
        <!-- body end -->
      </div>
    </div>
    <!-- </div> -->
    <!-- msg-list end -->

    <div class="msg-inputer">
      <!-- msg-inputer -->
      <div class="voice"></div>
      <input v-model="enterMsg" class="text" type="text" />

      <el-icon
        @click="sendMsg"
        style="
          font-size: 0.7rem !important;
          background: white;
          width: 1.6rem;
          border-radius: 0.7rem;
          height: 1rem;
          margin-right: 0.3rem;
        "
        color="#aaa"
        ><position
      /></el-icon>

      <el-icon
        @click="sendPicture"
        style="
          font-size: 0.7rem !important;
          background: white;
          width: 1.2rem;
          border-radius: 0.5rem;
          height: 1rem;
          margin-right: 0.3rem;
        "
        color="#aaa"
        ><Picture
      /></el-icon>

      <input
        id="uploadPicutre"
        @change="onFilesChange"
        type="file"
        multiple="true"
        style="display: none"
      />

      <!-- <div class="send-btn">
        <el-icon style="font-size: 0.6rem" color="#aaa"><position /></el-icon>
      </div> -->
    </div>
  </div>
</template>

<script>
import { getTestApi, getMsgListApi, sendMsgApi } from "@/api/webApi.js"
import { ElButton, ElMessage, ElLoading } from "element-plus"
import { Position, Picture } from "@element-plus/icons-vue"
import { dateFormatForList, scrollToBottom } from "@/utils/common.js"
import BaseListComponent from "@/pages/BaseListComponent.js"

export default {
  name: "SessionDetail",
  components: {
    Position,
    Picture,
  },
  mixins: [BaseListComponent],
  setup() {},
  created() {},
  mounted() {
    // let menu = document.getElementById('menu')
    // menu.style.top = 1000 +'px'
  },
  methods: {
    getMsgList: getMsgListApi,
    dateFormat: dateFormatForList,
    init() {},
    click(e){
      console.log(e.currentTarget) 
      console.log(e)
    },
    onListScroll(e){
        this.touchMenuVisible = false
    },
    onListClick(e){ 
    },
    touchstart(e, item) {
      // let e = window.event
      let t = new Date().getTime()
      this.touchStartAt = t
      this.touchEventTarget = e.currentTarget
      this.touchItem = item
      let targetTouch = e.targetTouches[0]
      this.targetTouch = targetTouch
      console.log(e)

      // console.log(targetTouch)
    },
    touchend() {
      let t = new Date().getTime()
      console.log("touchend:" + t)
      let a = this.touchEventTarget

      let scrollHeight = this.touchEventTarget.scrollHeight
      let clientHeight = this.touchEventTarget.clientHeight
      let offsetHeight = this.touchEventTarget.offsetHeight
      let offsetTop = this.touchEventTarget.offsetTop
      // console.log(scrollHeight)
      // console.log(offsetTop)
      console.log(
        "scrollHeight:" + document.getElementById("msg-window").scrollHeight
      )

      if (t - this.touchStartAt >= 500) {
        console.log(" touchend event")
        let menu = document.getElementById("menu")
        let right = this.targetTouch.pageX
        let top =this.targetTouch.pageY // offsetTop
        menu.style.position = 'fixed'
        menu.style.top = top + "px"
        menu.style.left = right + "px"
        this.touchMenuVisible = true
        this.allowDeleteMsg =false
        setTimeout(()=>{
           this.allowDeleteMsg =true
        },300)
        console.log({
          top,
          right,
        })
      }
      else
      {
        this.touchMenuVisible = false 
      }
    },
    deleteMsg() {
      if(this.allowDeleteMsg == false)
      {
        return
      }
      this.touchMenuVisible = false
      if (this.touchItem != null) {
        let elem = document.getElementById("msg-item-" + this.touchItem.id)
        let i = 0
        let n = setInterval(() => {
          if (i == 100) {
            this.dataList.splice(this.dataList.indexOf(this.touchItem), 1)
            setTimeout(() => {
              elem.style.display = "none"
            }, 30)
            clearInterval(n)
          }
          // let o = elem.style.opacity
          let _o = (100 - i) / 100.0
          console.log(_o)
          elem.style.opacity = _o
          i++
        }, 3)
      }
    },
    loadMsgBtnClick() {
      if (this.scroll.disabled || this.scroll.noMore) {
        console.log(
          "scroll.disabled:" +
            this.scroll.disabled +
            ", scroll.noMore:" +
            this.scroll.noMore
        )
        return
      }
      console.log("loadList,pageIndex:" + this.page.pageIndex)

      const loading = ElLoading.service({
        lock: true,
        text: "加载中",
        background: "rgba(0, 0, 0, 0.7)",
      })
      setTimeout(() => {
        loading.close()
      }, 5000)

      let methods = this.$options.methods
      this.scroll.loading = true
      this.scroll.disabled = true
      setTimeout(() => {
        // debugger
        // getMsgListApi(this.page).then(res=>{
        //   debugger
        // })

        methods[this.loadListMethod](this.page)
          .then((res) => {
            let { code, msg, data } = res
            let { rows, count } = data
            debugger
            console.log(res)
            if (code == 200) {
              if (rows.length == 0) {
                this.scroll.disabled = true
                this.scroll.noMore = true
                console.log("no more")
              } else {
                // this.dataList = this.dataList.concat(rows)
                // console.log(rows)
                rows = rows.reverse()
                // console.log(rows)

                for (let i = 0; i < rows.length; i++) {
                  this.dataList.unshift(rows[i])
                }
                this.page.pageIndex += 1
              }
            } else {
              // msg
            }
          })
          .catch((err) => {})
          .finally(() => {
            this.scroll.loading = false
            loading.close()
            this.scroll.disabled = false
          })
      }, 1000)
    },
    handleScroll() {},
    sendMsg() {
      if (this.enterMsg.trim() === "") {
        ElMessage({
          showClose: true,
          message: "内容不能为空",
          center: true,
          type: "error",
          // duration:3000000
        })
        return
      } else if (this.enterMsg.length > 1000) {
        ElMessage({
          showClose: true,
          message: "长度不能超过1000",
          center: true,
          type: "error",
          // duration:3000000
        })
        return
      }

      const loading = ElLoading.service({
        lock: true,
        text: "加载中",
        background: "rgba(0, 0, 0, 0.7)",
      })
      setTimeout(() => {
        loading.close()
      }, 5000)

      let params = {
        content: this.enterMsg,
      }
      setTimeout(() => {
        sendMsgApi(params)
          .then((res) => {
            let { code, msg, data } = res
            console.log(res)
            if (code == 200) {
              this.enterMsg = ""
              this.dataList.push(data)
              scrollToBottom(document.getElementById("msg-window"),30)
            } else {
              // msg
            }
          })
          .catch((err) => {})
          .finally(() => {
            loading.close()
          })
      }, 1000)
    },
    onFilesChange(e) {
      let { target, currentTarget } = e
      let { files } = target
      const allowTypes = [
        "image/png",
        "image/jpg",
        "image/jpeg",
        "image/bmp",
        "image/gif",
        "image/webp",
        "image/psd",
        "image/svg",
        "image/tiff",
      ]

      console.log(allowTypes)

      files.forEach((f) => {
        console.log(f)
      })

      // const allowTypes = ['image/jpeg','image/jpg','image/gif','image/png',]
      console.log("type of files" + typeof files)

      // debugger
    },
    sendPicture() {
      document.getElementById("uploadPicutre").click()
    },
  },
  data() {
    return {
      touchItem: null,
      touchEventTarget: null,
      touchMenuVisible: false,
      allowDeleteMsg:false,
      targetTouch: null,
      touchStartAt: 0,
      touchDuration: 0,
      loadListMethod: "getMsgList",
      enterMsg: "",
      uploadPicutre: {
        files: [],
        pictureUrlList: [],
      },
    }
  },
}
</script>

<style scoped>
.menu {
  width: 1.5rem;
  text-align: center;
  background: white;
  position: absolute;
  top: 100px;
  right: 10px;
  z-index: 11;
  font-size: 0.2rem;
  background: rgba(0, 0, 0, 0.7);
  border-radius: 0.1rem;
}
.menu .menu-item {
  border-bottom: 1px solid #ccc;
  padding: 0.2rem;
  color: white;
}
.menu .menu-item:last-child {
  border-bottom: none;
}

.load-msg-btn {
  width: 2.5rem;
  font-size: 0.3rem;
  text-align: center;
  border-radius: 0.5rem;
  margin: auto;
  line-height: 0.75rem;
  background: white;
  margin-bottom: 0.3rem;
}
.main-container {
  /* display: flex;
    flex-direction:column; */
  width: 100%;
}
.msg-window {
  position: fixed;
  background: #ccc;
  height: 100%;
  width: 100%;
  padding: 0.45rem;
  overflow: scroll;
  padding-bottom: 1.5rem;
}
.msg-window .msg-list {
}
.msg-window .frame {
}
.msg-window .msg-item {
  /* display: flex;
  align-items: center; */
  width: 100%;
  margin: auto;
  margin-bottom: 1rem;
  /* flex-direction: column; */
}
.msg-window .row-reverse {
  flex-direction: row-reverse;
}
.msg-window .row-reverse .name {
  display: none;
}
.msg-window .msg-item .msg-item-top {
  /* display: flex; */
  color: 333;
}
.msg-window .msg-item .msg-item-top .date {
  font-size: 0.3rem;
  text-align: center;
  margin-bottom: 0.3rem;
}
.msg-window .msg-item .body {
  display: flex;
  /* align-items: center; */
}

.msg-window .msg-item .body .avatar {
  width: 1rem;
  height: 1rem;
  border-radius: 50%;
  background: white;
  /* margin-left: 0.5rem; */
  /* margin-right: 0.2rem; */
  margin: 0 0.3rem 0 0;
}
.msg-window .row-reverse .avatar {
  margin: 0 0 0 0.3rem !important;
}
.msg-window .msg-item .body .name {
  font-size: 0.3rem;
  color: 333;
  padding-bottom: 0.1rem;
  margin-top: -0.2rem;
}
.msg-window .msg-item .body .msg-info {
  background: white;
  padding: 0.5rem;
  font-size: 0.3rem;
  border-radius: 0.13rem;
  color: 333;
  position: relative;
}
.msg-window .row-reverse .msg-info::before {
  right: -0.36rem !important;
  left: inherit !important;
  top: 0.5rem !important;
  border-color: transparent transparent transparent white !important;
}
.msg-window .row-reverse .msg-info::after {
  /* left: -0.36rem !important;
  border-color: transparent   transparent white  transparent; */
}

.msg-window .msg-item .body .msg-info::before {
  /* width: 0.5rem;
  height: 0.5rem;
  border-radius: 1rem; */

  content: "";
  width: 0;
  height: 0;
  position: absolute;

  top: 0.2rem;
  left: -0.36rem;
  border: solid 8px;
  border-color: transparent white transparent transparent;
  font-size: 0;

  /* position: absolute;
             content: "";
            width: 0;
            height: 0;
             right: 100%;
            top: 38px;
           border-top: 13px solid transparent;
            border-right: 26px solid #088cb7;
             border-bottom: 13px solid transparent; */
}
.msg-window .msg-item .body .left {
}

.msg-window .msg-item .body .right {
  /* flex: 65%; */
  max-width: 85%;
}

.msg-window .msg-pop {
  background: blue;
  padding: 0.5rem;
}
.msg-window .msg-pop::before {
}
.msg-inputer {
  background: #c8c9cc;
  position: absolute;
  bottom: 0;
  z-index: 1111111;
  width: 100%;
  height: 1.5rem;
  display: flex;
  overflow: hidden;
  padding-bottom: 0.2rem;
  align-items: flex-end;
}

.msg-inputer .voice {
}
.msg-inputer .text {
  background: white;
  width: 7.7rem;
  height: 1rem;
  border-radius: 1rem;
  margin-left: 0.3rem;
  margin-right: 0.3rem;
  padding-left: 0.5rem;
  padding-right: 0.5rem;
  font-size: 0.4rem;
}
.msg-inputer .text:focus {
  border: 1px solid #a0cfff !important;
  outline: 1px solid #a0cfff !important;
}
.msg-inputer .send-btn {
  width: 2rem;
  font-size: 0.5rem;
  height: 1rem;
  border-radius: 1rem;
  background: white;
  text-align: center;
  line-height: 1rem;
}
</style>
